package com.ly.hotel.view.common;

import com.ly.hotel.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

public class MyProcessDialog extends Activity{

	private ProgressDialog progressDialog = null;
	TextView waitText;
	String msg;
	Context mContext;
	int timeout;
	LayoutInflater mInflater;
	
	/**
	 * @param c
	 * @param str   	提示信息
	 * @param timeout 	窗口超时时间(单位毫秒)
	 */
	public MyProcessDialog(Context c, String str ,int timeout) {
		this.timeout = timeout;
		this.mContext = c;
		this.msg = str;
		mInflater = LayoutInflater.from(mContext);
	}
	
	public void showProgressDialog() {
//		System.out.println("showProgressDialog");
		// 如果已经在显示了，就退出当前的，再次创建一个
		if (progressDialog != null)
			if (progressDialog.isShowing()){
				progressDialog.cancel();
			}

		/* 设置动画 */
		mInflater = LayoutInflater.from(mContext);
		View v = mInflater.inflate(R.layout.my_progress_dialog_view, null);
		waitText = (TextView) v.findViewById(R.id.progress_waitting_text);
		waitText.setText(msg);

		ImageView spaceshipImage = (ImageView) v.findViewById(R.id.progress_waitting_img);
		// 加载动画
		Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(mContext, R.anim.animation);
		// 使用ImageView显示动画
		spaceshipImage.startAnimation(hyperspaceJumpAnimation);
		/* 设置动画 end */

		progressDialog = new ProgressDialog(mContext);
		progressDialog.setCancelable(false);
		progressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
		progressDialog.show();
		progressDialog.setContentView(v);// setContentView可以是一个简单的TextView

		// 另一种方式实现自动关闭
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(timeout);
					closeProgressDialog();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			}
		}).start();
	}

	public void closeProgressDialog() {
		// 调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss方法不会回掉
		if (progressDialog != null)
			if (progressDialog.isShowing())
				progressDialog.cancel();
	}

}
